﻿namespace LeetCodeProject._0001_1000._001_100._61_70._063;

public class Solution
{
    public int UniquePathsWithObstacles(int[][] obstacleGrid)
    {
        var n = obstacleGrid.Length;
        var m = obstacleGrid[0].Length;
        var paths = new int[m];
        for (int i = 0; i < m; i++)
        {
            paths[i] = (i > 0 && paths[i - 1] == 0) || obstacleGrid[0][i] != 0 ? 0 : 1;
        }

        for (int i = 1; i < n; i++)
        {
            var line = obstacleGrid[i];
            if (line[0] != 0)
                paths[0] = 0;
            for (int j = 1; j < m; j++)
            {
                if (line[j] != 0)
                {
                    paths[j] = 0;
                }
                else
                {
                    paths[j] += paths[j - 1];
                }
            }
        }

        return paths[m - 1];
    }
}